import { AsyncValidator, NG_ASYNC_VALIDATORS, ValidationErrors, AbstractControl, FormBuilder, FormGroup, Validators, ControlValueAccessor, NG_VALUE_ACCESSOR, FormControl } from '@angular/forms';
import { Observable, Subject, ReplaySubject, from, of, range } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Injectable, Component, OnInit, ViewChild, Input, Renderer2, forwardRef, Injector, Inject, DoCheck } from '@angular/core';
import { ResData } from '@/core';

@Injectable({
  providedIn: 'root',
})
export class HasUsernameValidatorService implements AsyncValidator {
  constructor(@Inject('UserService') private userService) {}
  validate(ctrl: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> {
    return this.userService.hasUser(ctrl.value).pipe(
      map((res: ResData) => res.data),
      map(user => {
        return !!user ? null : { has: true };
      }),
      catchError(() => null),
    );
  }
}
